
$().ready( function() {

	// 登录窗口显示
	$.loginWindowShow = function () {
		$("#loginWindow").jqmShow();
	}

	// 必须会员登录才允许的操作，若未登录则显示登录窗口
	$("a.mustMemberLogin").click( function(event) {
		if ($.cookie("loginMemberUsername") == null) {
			$.cookie("redirectionUrl", $(this).attr("href"), {path: "/"});
			$("#loginWindow").jqmShow();
			return false; 
		}
	});
	
	// 登录窗口/b/customer/ajaxLogin
	var shopId=$("#shopId").val();
	//alert(shopId);
	var loginWindowHtml = '<div id="loginWindow" class="loginWindow"><div class="windowTop"><div class="windowTitle">会员登录</div><a class="windowClose loginWindowClose" href="#" hidefocus="true"></a> </div> <div class="windowMiddle"> ' +
		'<form id="loginWindowForm" action="" method="post"><ul class="login"><li><span class="user"></span><span>' +
		'<input placeholder="手机号码" id="phone" name="phone" class="formText" /></span></li><li><span class="password"></span><span>' +
		'<input placeholder="密码" id="password" type="password" name="member.password" class="formText" /></span></li><li class="forget"></li><li class="login_h"><span class="login_button">' +
		'<a id="" class="loginSubmit" hidefocus="true" style="text-align: center">登 录</a></span></li></ul>' +
		'<input type="hidden" name="shopId" value='+shopId+'></form></div><div class="windowBottom"></div></div>';

	$("body").prepend(loginWindowHtml);
	
	// 登录悬浮窗口
    $("#loginWindow").jqm({
		modal: true,// 是否开启模态窗口
		overlay: 10,// 屏蔽层透明度
		trigger: ".showLoginWindow",// 激活元素
		closeClass: "loginWindowClose",// 关闭按钮
		onHide: function(hash) {
			$("#loginWindowForm").resetForm();
			$.cookie("redirectionUrl", null, {path: "/"});
    		hash.o.remove();
    		hash.w.fadeOut();
    	},
    	onShow: function(hash){
    		hash.w.fadeIn();
    		loginWindowCaptchaImageRefresh();
	    }
	}).jqDrag(".windowTop");
	
	// 表单验证
	$("#loginWindowForm").validate({
		invalidHandler: function(form, validator) {
			$.each(validator.invalid, function(key, value){
				$.tip(value);
				return false;
			});
		},
		errorPlacement:function(error, element) {},
		submitHandler: function(form) {
			$("#loginWindowSubmit").attr("disabled", true);
			$("#loginWindowForm").ajaxSubmit({
				dataType: "json",
				success: function(data) {
					if (data.status == "success") {
						$.tip(data.status, data.message);
						$.flushHeaderInfo();
						$.flushCartItemList();
						var redirectionUrl = $.cookie("redirectionUrl");
						$("#loginWindow").jqmHide();//登录成功后,登录悬浮窗口隐藏
						if(redirectionUrl != null && redirectionUrl != "") {
							location.href = redirectionUrl;
						}
					} else {
						loginWindowCaptchaImageRefresh();
						$("#loginWindowCaptcha").val("");
						$.tip(data.status, data.message);
					}
					$("#loginWindowSubmit").attr("disabled", false);
				}
			});
		}
	});
	
	// 刷新验证码图片
	function loginWindowCaptchaImageRefresh() {
		$("#loginWindowCaptchaImage").attr("src", JFinalshop.base + "/captcha?timestamp=" + (new Date()).valueOf() + "&width=100&height=45&fontsize=35");
	}
	
	// 点击刷新验证码图片
	$("#loginWindowCaptchaImage").click( function() {
		loginWindowCaptchaImageRefresh();
	});
	
	// 刷新验证码图片
	function loginCaptchaImageRefresh() {
		$("#loginCaptchaImage").attr("src", JFinalshop.base + "/captcha?timestamp=" + (new Date()).valueOf() + "&width=100&height=45&fontsize=35");
	}
	
	// 点击刷新验证码图片
	$("#loginCaptchaImage").click( function() {
		loginCaptchaImageRefresh();
	});
	
	// 表单验证
	$("#loginForm").submit(function() {
		if ($("#loginUsername").val() == "") {
			$.message("请输入您的用户名!");
			return false;
		}
		if ($("#loginPassword").val() == "") {
			$.message("请输入您的密码!");
			return false;
		}
		if ($("#loginCaptcha").val() == "") {
			alert($("#loginCaptcha").val());
			$.message("请输入您的验证码!");
			return false;
		}
	})

    $(".loginSubmit").click(function () {
        var phone=$("input[name='phone']").val();
        var password=$("#password").val();
        console.log(phone);
        console.log(password);
        if(!username){
            $.tip("请输入用户名!");
            return;
        }
        if(!password){
            $.tip("请输入用密码!");
            return;
        }
        $.ajax({
            url: "/b/pc/customer/ajaxLogin",
            type: 'GET',
            data:{
                "phone":phone,
                "password":password
            },
            success: function(data){
                if(data.code == 1){
                    $.tip(data.desc);
                }else if (data.code == 0){
                    $.tip("会员登录成功!")
                    setTimeout(function(){
                        location.reload();
                    },1500);

                }
            }
        });

    });

});